<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control 
            class="tab-control-fixed" 
            :titles="titles" 
            @tabClick="tabClick" 
            ref="tabControlFixed"
            v-show="isTabFixed"/>
    <scroll class="home-content" 
            ref="scroll" 
            :probe-type="3" 
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends"/>
      <feature-view @featureImageLoad="featureImageLoad"/>
      <tab-control 
            class="tab-control" 
            :titles="titles" 
            @tabClick="tabClick" 
            ref="tabControl"/>
      <!-- 数据获取到，但DOM未更新，会影响better-scroll滚动，需要在Scroll组件里使用observeDOM -->
      <goods-list v-if="showGoods.length !== 0" :goods="showGoods"/>
    </scroll>
    <!-- .native 监听组件的原生事件 -->
    <back-top @click.native="backTopClick" v-show="isShowBackTop"/>
  </div>
</template>

<script>
  import HomeSwiper from './childComps/HomeSwiper'
  import RecommendView from './childComps/RecommendView'
  import FeatureView from './childComps/FeatureView'
  
  import NavBar from 'components/common/navbar/NavBar'
  import Scroll from 'components/common/scroll/Scroll'
  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from 'components/content/goods/GoodsList'

  import {getHomeMultidata, getHomeGoods} from 'network/home'

  import {backTopMixin} from 'common/mixin'

  export default {
    name: 'Home',
    components: {
      HomeSwiper,
      RecommendView,
      FeatureView,

      NavBar,
      Scroll,
      TabControl,
      GoodsList
    },
    mixins: [backTopMixin],
    data() {
      return {
        banners: [],
        recommends: [],
        titles: ['流行', '古风', '欧美'],
        goods: {
          '流行': {page: 0, list: []},
          '古风': {page: 0, list: []},
          '欧美': {page: 0, list: []}
        },
        currentType: '流行',
        tabOffsetTop: 0,
        isTabFixed: false,
        saveY: 0  // 保存离开时的位置信息
      }
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    methods: {
      // 事件监听相关方法
      // 点击tab栏
      tabClick(i) {
        // console.log(i)
        this.currentType = this.titles[i]
        this.$refs.tabControlFixed.currentIndex = i
        this.$refs.tabControl.currentIndex = i
      },
      // 判断滚动位置，判断是否显示回到顶部按钮
      contentScroll(position) {
        // 1. 判断BackTop是否显示
        // position.y是负值， 需要使用绝对值进行判断
        this.listenShowBackTop(position)

        // 2. 决定tabControl是否吸顶(position: fixed)
        this.isTabFixed = (-position.y) > this.tabOffsetTop
      },
      // 加载更多
      loadMore() {
        this.getHomeGoods(this.currentType)
      },
      // 监听轮播图是否加载完成，获得tabControl的offsetTop
      swiperImageLoad() {
        // 获取tabControl的offsetTop
        // 所有组件都有一个属性 $el：用于获取组件中的元素
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
        // console.log(this.tabOffsetTop);
      },
      // 监听图片加载完成，获取tabOffsetTop
      featureImageLoad() {
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
      },

      // 网络请求相关方法
      getHomeMultidata() {
        getHomeMultidata().then(res => {
          // console.log(res.data);
          this.banners = res.data.banner.list
          this.recommends = res.data.recommend.list
        }).catch(err => {
          console.log(err)
        })
      },
      getHomeGoods(cat) {
        const offset = this.goods[cat].page + 1
        getHomeGoods(cat, offset).then(res => {
          // console.log(res)
          this.goods[cat].list.push(...res.playlists)
          this.goods[cat].page += 1

          // 完成上拉加载更多
          // 数据请求完毕后,调用 finishPullUp() 告诉 BetterScroll 准备好下一次的 pullingUp 钩子
          this.$refs.scroll.finishPullUp()
        })
      }
    },
    created() {
      // 1. 请求多个数据
      this.getHomeMultidata()

      // 2. 请求商品数据
      this.getHomeGoods('流行')
      this.getHomeGoods('古风')
      this.getHomeGoods('欧美')
    },
    activated() {
      this.$refs.scroll.refresh()
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
    },
    deactivated() {
      this.saveY = this.$refs.scroll.getScrollY()
    }
  }
</script>

<style scoped>
  #home {
    position: relative;
    /* 必须给wrapper设置视口高度，否则会出现滚动到底部留白的问题 */
    height: 100vh;
    /* padding-top: 44px; */
  }

  .home-content {
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
    /* height: 500px; */
  }

  .home-nav {
    /* 使用浏览器原生滚动时，为了让导航不跟随一起滚动 */
    /* position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9; */
    background-color: var(--color-tint);
    color: #fff;
  }

/*   .tab-control {
    position: sticky;
    top: 40px;
  } */

  .tab-control-fixed {
    position: relative;
    z-index: 9;
  }
</style>